# -!- coding: utf-8 -!-
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
#作者：cacho_37967865
#博客：https://blog.csdn.net/sinat_37967865
#文件：get_stock_report.py
#日期：2019-04-29
#备注：获取东方财富网个股研报信息
 http://data.eastmoney.com/report/stock.jshtml
 
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

import requests
import re
import time
from pacong.stock.stock_config import db,cursor,headers
from pacong.stock.stock_base import get_stock_no_price
from pycacho.cachobase.deal_float import float_deal
from pycacho.cachobase.logger import Logger

logger = Logger("get_stock_report").get_log()

# 在requests做请求的时候，为了避免ssl认证，可以将requests.get(url,verify=False), 但是这么设置会带来一个问题，日志中会有大量的warning信息, 可以配置下面4行
session = requests.Session()
session.keep_alive = False
requests.adapters.DEFAULT_RETRIES = 5
requests.packages.urllib3.disable_warnings()

def create_table():
    sql1 = 'drop table if exists stock_report;'
    sql2 = 'create table stock_report(stock_code varchar(10),stock_name varchar(10),report_type varchar(10),price_min float,price_max float,report_title varchar(100),org_name varchar(50),publish_date varchar(50),report_num float,' \
           'PRIMARY KEY (stock_code,org_name,publish_date));'   # 股票研报信息
    cursor.execute(sql1)
    cursor.execute(sql2)
    db.commit()


def report_list():
    #open_website()
    url = 'http://data.eastmoney.com/report/stock.jshtml'
    resp = requests.get(url, headers=headers)

    data = resp.text
    info = re.findall(r'initdata =(.*)"};', data)[0] + '"}'
    reports = eval(info)
    report_list = reports['data']
    for report in report_list:
        stock_code = report['stockCode']
        stock_name = report['stockName']
        report_type = report['emRatingName']   # 买入、增持
        if report['indvAimPriceL'] == '':
            price_min = 0.00    # 目标价最低
        else:
            price_min = float_deal(report['indvAimPriceL'])
        if report['indvAimPriceT'] == '':
            price_max = 0.00
        else:
            price_max = float_deal(report['indvAimPriceT'])  # 目标价最高
        report_title = report['title']
        org_name = report['orgSName']           #
        publish_date = report['publishDate'][0:10]   #
        report_num = report['count']           # 近一月个股研报数
        #print(report)
        #print(stock_code,stock_name,report_type,price_min,price_max,report_title,org_name,publish_date,report_num)
        report_to_mysql(stock_code, stock_name, report_type, price_min, price_max, report_title, org_name, publish_date,report_num)

def single_report(stock_no):
    # https://data.eastmoney.com/report/300418.html 选时间
    url = 'http://reportapi.eastmoney.com/report/list?pageNo=1&pageSize=10&code={0}&beginTime=2023-01-01&endTime=2024-04-24&qType=0'.format(stock_no)
    resp = requests.get(url, headers=headers).json()
    time.sleep(2)
    report_list = resp['data']
    for report in report_list:
        stock_code = report['stockCode']
        stock_name = report['stockName']
        report_type = report['emRatingName']   # 买入、增持
        if report['indvAimPriceL'] == '':
            price_min = 0.00    # 目标价最低
        else:
            price_min = float_deal(report['indvAimPriceL'])
        if report['indvAimPriceT'] == '':
            price_max = 0.00
        else:
            price_max = float_deal(report['indvAimPriceT'])  # 目标价最高
        report_title = report['title']
        org_name = report['orgSName']           #
        publish_date = report['publishDate'][0:10]   #
        report_num = report['count']           # 近一月个股研报数
        print(report)
        #print(stock_code,stock_name,report_type,price_min,price_max,report_title,org_name,publish_date,report_num)
        report_to_mysql(stock_code, stock_name, report_type, price_min, price_max, report_title, org_name, publish_date,report_num)


# 股票研报信息
def report_to_mysql(stock_code,stock_name,report_type,price_min,price_max,report_title,org_name,publish_date,report_num):
    sql = "insert into stock_report (stock_code,stock_name,report_type,price_min,price_max,report_title,org_name,publish_date,report_num,create_time) " \
          "values ('%s','%s','%s','%s','%s','%s','%s','%s','%s',now());" \
          % (stock_code,stock_name,report_type,price_min,price_max,report_title,org_name,publish_date,report_num)
    try:
        cursor.execute(sql)
    except Exception as e:
        # 发生错误时回滚
        db.rollback()
        print(str(e))
    else:
        db.commit()
        logger.info('插入研报信息成功 %s！',stock_name + '-->' + report_title )


if __name__ == '__main__':
    #create_table()
    #report_list()
    for stock in get_stock_no_price():
        print(stock)
        single_report(stock)